<template>
	<section class="vbox">
		<section class="scrollable padder">
			<section class="panel panel-default m-t-sm">
				<header class="panel-heading">
					菜单管理
					<ul class="nav nav-tabs pull-right no-border">
						<li class="m-r-sm m-t-xs">
							<el-input placeholder="输入菜单名称关键字" v-model="keywords" clearable>
								<template #append>
									<el-button slot="append" type="success" icon="icon-magnifier icon" @click="query(1)"></el-button>
								</template>
							</el-input>
						</li>
						<li class="m-r-md m-t-xs">
							<button class="btn btn-success" @click="model={}; model.isEnabled='1'; dialogFormVisible = true"><i class="fa fa-plus-circle"></i> 新增</button>
						</li>
					</ul>
				</header>
				<div class="panel-body">
					<el-table size="medium" :data="menuList" stripe style="width: 100%" border>
						<el-table-column fixed type="index" width="auto" :index="indexMethod">
						</el-table-column>
						<el-table-column prop="menuCode" label="菜单编码" width="120">
						</el-table-column>
						<el-table-column prop="menuTitle" label="菜单名称" width="120">
						</el-table-column>
						<el-table-column prop="url" label="地址">
						</el-table-column>
						<!-- <el-table-column prop="urlType" label="地址类型">
						</el-table-column>
						<el-table-column prop="urlTarget" label="外链打开方式">
						</el-table-column> -->
						<el-table-column prop="icon" label="图标" width="60" align="center">
							<template v-slot="scope">
								<i :class="scope.row.icon"></i>
							</template>
						</el-table-column>
						<el-table-column prop="componentPath" label="组件路径">
						</el-table-column>
						<el-table-column prop="isHidden" label="是否隐藏" width="100" align="center">
							<template v-slot="scope">
								<el-switch v-model="scope.row.isHidden" active-color="#13ce66" inactive-color="#ff4949" active-value="1"
								 inactive-value="0">
								</el-switch>
							</template>
						</el-table-column>
						<el-table-column prop="isEnabled" label="是否可用" width="100" align="center">
							<template v-slot="scope">
								<el-switch v-model="scope.row.isEnabled" active-color="#13ce66" inactive-color="#ff4949" active-value="1"
								 inactive-value="0">
								</el-switch>
							</template>
						</el-table-column>
						<el-table-column prop="updateTime" label="更新时间" width="160">
						</el-table-column>
						<el-table-column align="center" label="操作">
							<template v-slot="scope">
								<el-button type="text" @click="editMenu(scope.row)">编辑</el-button>
								<el-divider direction="vertical"></el-divider>
								<el-button type="text" @click="deleteMenu(scope.row.id)">删除</el-button>
								<el-divider direction="vertical"></el-divider>
								<el-button type="text" @click="deleteMenu(scope.row.id)">按钮</el-button>
							</template>
						</el-table-column>
					</el-table>

					<el-pagination 
						background 
						layout="prev, pager, next" :pager-count="5" 
						:total="total" :page-size="pageSize"
						@current-change="query">
					</el-pagination>
				</div>

				<el-dialog :title="model.id==null ? '新增': '编辑'" v-model="dialogFormVisible" top="5vh">
					<form class="form-horizontal" action="#" method="get" onsubmit="return false;" data-validate="parsley">
						<div class="form-group">
							<label class="col-sm-2 control-label">父菜单</label>
							<div class="col-sm-10">
								<input type="text" v-model.trim="model.parentId" class="form-control parsley-validated" required="true">
							</div>
						</div>
						<div class="line line-dashed b-b line-lg pull-in"></div>
						<div class="form-group">
							<label class="col-sm-2 control-label">菜单编码</label>
							<div class="col-sm-10">
								<input type="text" v-model.trim="model.menuCode" class="form-control parsley-validated" required="true">
							</div>
						</div>
						<div class="line line-dashed b-b line-lg pull-in"></div>
						<div class="form-group">
							<label class="col-sm-2 control-label">地址</label>
							<div class="col-sm-10">
								<input type="text" v-model.trim="model.url" class="form-control parsley-validated" required="true">
							</div>
						</div>
						<div class="line line-dashed b-b line-lg pull-in"></div>
						<div class="form-group">
							<label class="col-sm-2 control-label">组件路径</label>
							<div class="col-sm-10">
								<input type="text" v-model.trim="model.componentPath" class="form-control parsley-validated" required="true">
							</div>
						</div>
						<div class="line line-dashed b-b line-lg pull-in"></div>
						<div class="form-group">
							<label class="col-sm-2 control-label">是否隐藏</label>
							<div class="col-sm-10">
								<el-switch v-model="model.isHidden" active-color="#13ce66" inactive-color="#ff4949" active-value="1"
								 inactive-value="0">
								</el-switch>
							</div>
						</div>
						<div class="line line-dashed b-b line-lg pull-in"></div>
						<div class="form-group">
							<label class="col-sm-2 control-label">菜单名称</label>
							<div class="col-sm-10">
								<input type="text" v-model.trim="model.menuTitle" class="form-control parsley-validated" required="true">
							</div>
						</div>
						<div class="line line-dashed b-b line-lg pull-in"></div>
						<div class="form-group">
							<label class="col-sm-2 control-label">图标</label>
							<div class="col-sm-10">
								<input type="text" v-model.trim="model.icon" class="form-control parsley-validated" required="true">
							</div>
						</div>
						<div class="line line-dashed b-b line-lg pull-in"></div>
						<div class="form-group">
							<label class="col-sm-2 control-label">地址类型</label>
							<div class="col-sm-10">
								<div class="radio i-checks radio-inline">
									<label>
										<input type="radio" name="urlType" v-model.trim="model.urlType" value="L">
										<i></i> 内链
									</label>
								</div>
								<div class="radio i-checks radio-inline">
									<label>
										<input type="radio" name="urlType" v-model.trim="model.urlType" value="O">
										<i></i> 外链
									</label>
								</div>
							</div>
						</div>
						<div class="line line-dashed b-b line-lg pull-in"></div>
						<div class="form-group">
							<label class="col-sm-2 control-label">外链打开方式</label>
							<div class="col-sm-10">
								<el-select v-model="model.urlTarget" placeholder="请选择">
									<el-option v-for="item in urlTargets" :key="item.value" :label="item.label" :value="item.value">
									</el-option>
								</el-select>
							</div>
						</div>
						<div class="line line-dashed b-b line-lg pull-in"></div>
						<div class="form-group">
							<label class="col-sm-2 control-label">是否可用</label>
							<div class="col-sm-10">
								<el-switch v-model="model.isEnabled" active-color="#13ce66" inactive-color="#ff4949" active-value="1"
								 inactive-value="0">
								</el-switch>
							</div>
						</div>
						<div class="line line-dashed b-b line-lg pull-in"></div>
						<div class="form-group">
							<label class="col-sm-2 control-label">是否缓存页面</label>
							<div class="col-sm-10">
								<el-switch v-model="model.isKeepAlive" active-color="#13ce66" inactive-color="#ff4949" active-value="1"
								 inactive-value="0">
								</el-switch>
							</div>
						</div>
						<div class="line line-dashed b-b line-lg pull-in"></div>
						<div class="form-group">
							<div class="col-sm-10 col-sm-offset-2">
								<button type="submit" class="btn btn-success pull-right" @click="submit()">提交</button>
							</div>
						</div>
					</form>
				</el-dialog>

			</section>
		</section>
	</section>

</template>

<script>
	export default {
		components: {
			name: 'menu'
		},
		data() {
			return {
				dialogFormVisible: false,
				menuList: [],
				total: 0, // 记录总条数
				pageSize: 10, // 每页显示条数
				pageNo: 1, // 当前的页数
				keywords: '',
				urlTargets: [{
					value: '1',
					label: '_blank'
				}, {
					value: '2',
					label: '_parent'
				}, {
					value: '3',
					label: '_self'
				}, {
					value: '4',
					label: '_top'
				}, {
					value: '5',
					label: 'framename'
				}],
				model: {
					code: '',
					name: ''
				}
			}
		},
		mounted() {
			this.query(1);
		},
		methods: {
			indexMethod(index) {
				return this.pageSize * (this.pageNo - 1) + index + 1;
			},
			submit() {
				if (this.model.id == null) {
					this.$api.menu.add(this.model).then(data => {
						this.model.id = data.id;
						this.query(1);
					});
				} else {
					this.$api.menu.update(this.model);
				}
				this.dialogFormVisible = false;
			},
			query(pageNo) {
				this.pageNo = null == pageNo ? 1 : pageNo;
				this.$api.menu.query({
					menuTitle: this.keywords,
					pageSize: this.pageSize,
					pageNo: this.pageNo,
					parentCode: 0
				}).then(data => {
					this.menuList = data.rows;
					this.pageNo = data.pageNo;
					if (1 == this.pageNo) {
						this.total = data.total;
					}
				});
			},
			editMenu(model) {
				this.model = model;
				this.dialogFormVisible = true;
			},
			deleteMenu(id) {
				this.$confirm('您确定要执行删除操作吗？', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					this.$api.menu.delete(id).then(data => {
						this.query(this.pageNo);
					});
				});
			}
		}
	}
</script>

<style>
</style>
